package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import jdk.net.SocketFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by WF on 2019/9/17 10:26
 */
@RestController
@RequestMapping("label")
@CrossOrigin            //主要是为了解决跨域请求问题
@RefreshScope           //主要是为了解决自定义变量的值的变化，不可以使用bus的问题
public class LabelController {

    @Autowired
    private LabelService labelService;
    @Value("${ip}")
    private String ip;

    //1.查询所有的标签列表
    @GetMapping
    public Result findAll(){
        //1.查询数据库
        List<Label> labels =  labelService.findAll();
        //2.返回结果
        return new Result(true,StatusCode.OK,"查询所有标签成功！",labels);
    }
    //2.根据id查询标签
    @GetMapping("/{labelId}")
    public Result findOne(@PathVariable String labelId){
        //2.1）根据标签id标签标签对象
        Label label = labelService.findOne(labelId);
        System.out.println("ip:" + ip);
        //2.2)返回结果
        return new Result(true,StatusCode.OK,"查询标签成功！",label);
    }
    //3.添加标签
    @PostMapping
    public Result add(@RequestBody Label label){
        //3.1)将新的标签添加到数据库中
        labelService.add(label);
        //3.2)返回结果
        return new Result(true,StatusCode.OK,"添加标签成功！");
    }
    //4.修改标签
    @PutMapping("{labelId}")
    public Result update(@PathVariable String labelId,@RequestBody Label label){
        //4.1）将当前的标签id与标签对象绑定
        label.setId(labelId);
        //4.2)执行修改操作
        labelService.update(label);
        //4.3)返回结果
        return new Result(true,StatusCode.OK,"修改标签成功！");
    }
    //5.删除标签
    @DeleteMapping("{labelId}")
    public Result delete(@PathVariable String labelId){
        //5.1)执行删除操作
        labelService.delete(labelId);
        //5.2)返回结果
        return new Result(true,StatusCode.OK,"删除标签成功！");
    }
    //6.条件查询带分页
    @PostMapping("search/{page}/{size}")
    public Result search(@PathVariable int page,@PathVariable int size,@RequestBody Label label){
        //6.1)根据当前页及每页大小和搜索关键字查询分页信息
        PageResult<Label> pageResult = labelService.search(page,size,label);
        //6.2)返回结果
        return new Result(true,StatusCode.OK,"条件带分页查询成功！",pageResult);
    }
    //7.条件查询
    @PostMapping("search")
    public Result searchByCond(@RequestBody Label label){
        //7.1)根据查询条件查询标签列表
        List<Label> labelList = labelService.searchByCond(label);
        //7.2)返回结果
        return new Result(true,StatusCode.OK,"条件查询成功！",labelList);
    }
}
